package polyu.comp;

import android.app.Activity;
import android.os.Bundle;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.animation.*;

import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.series.XYSeries;
import com.androidplot.xy.*;
 
import java.text.DecimalFormat;
import java.util.*;
 
public class RankActivity extends Activity
{
 
    @Override
	protected void onResume() {
    	this.UpdatePlot();
		super.onResume();
	}

	private XYPlot allPlot;
    private XYPlot rankPlot;
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rank_layout);
 
        // Initialize our XYPlot reference:
        allPlot = (XYPlot) findViewById(R.id.allPlot);
        rankPlot = (XYPlot) findViewById(R.id.rankPlot);
        
        rankPlot.setRangeLabel("");
        rankPlot.getLayoutManager().remove(rankPlot.getDomainLabelWidget());
        rankPlot.getLayoutManager().remove(rankPlot.getRangeLabelWidget());
        
        this.UpdatePlot();
    }
    
    public void UpdatePlot(){
    	
    	allPlot.clear();
    	rankPlot.clear();
    	
    	 Vector<ActionItem> items = DataManager.instance().getItems();
    	 
    	 HashMap<String, Integer> map = new HashMap<String, Integer>();
    	 
    	 for(ActionItem i : items) {
    		 if(! map.containsKey(i.uid)) {
    			 map.put(i.uid, i.length);
    		 } else {
    			 int t = map.get(i.uid);
    			 t += i.length;
    			 map.put(i.uid, t);
    		 }
    	 }
    	 
    	 LinkedList<Integer> al = new LinkedList<Integer>();
    	 StringBuilder sb = new StringBuilder();
    	 for (String k : map.keySet()) {
    		 sb.append(k);
    		 sb.append(" . ");
    		 al.add(map.get(k));
    	 }
    	 al.addFirst(0);
    	 al.addLast(0);
  
         // Turn the above arrays into XYSeries:
         XYSeries s = new SimpleXYSeries(
                 al,         
                 SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,
                 sb.toString());
         
         // Add series1 to the xyplot:
         rankPlot.addSeries(s, new BarFormatter(
                 Color.rgb(200, 200, 0),                   // line color
                 Color.rgb(0, 100, 0)));
         rankPlot.setDomainStepMode(XYStepMode.INCREMENT_BY_VAL);
         rankPlot.disableAllMarkup();
         
         // Set for individual activity.
         HashMap<String, LinkedList<Integer> > maps = new HashMap<String, LinkedList<Integer> >();
         
    	 for(ActionItem i : items) {
    		 if(! maps.containsKey(i.uid)) {
    			 LinkedList<Integer> l = new LinkedList<Integer>();
    			 l.add(i.length);
    			 maps.put(i.uid, l);
    		 } else {
    			 LinkedList<Integer> l = maps.get(i.uid);
    			 l.add(i.length);
    			 maps.put(i.uid, l);
    		 }
    	 }
    	 
    	 Vector<XYSeries> v = new Vector<XYSeries>();
    	 
    	 for (String skey : maps.keySet()) {
    		 XYSeries sr = new SimpleXYSeries(
    				 maps.get(skey),
    				 SimpleXYSeries.ArrayFormat.Y_VALS_ONLY,
    	             skey);
    		 v.add(sr);
    	 }
    	 
    	 Integer cs[] = {Color.WHITE, Color.CYAN, Color.LTGRAY, Color.RED, Color.GRAY, Color.BLUE, Color.GREEN, Color.YELLOW};
    	 
    	 for (XYSeries xy : v) {
    		 
    		 Random rand = new Random();
    		 
    		 Integer curc = cs[rand.nextInt(cs.length)];
    		 
    		 allPlot.addSeries(
    				 xy, 
    				 new LineAndPointFormatter(curc, null, null));
    	 }
    	 
    	 allPlot.setBackgroundColor(Color.BLACK);

        // Reduce the number of range labels
        allPlot.setTicksPerRangeLabel(3);
        allPlot.disableAllMarkup();
        
        Animation am = new ScaleAnimation( 0.3f, 1.0f, 0.3f, 1.0f
        		, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        am.setDuration(1000);
        
        allPlot.setAnimation(am);
        rankPlot.setAnimation(am);
        am.startNow();
    }
}